package com.dominator.utils.sms.dianji;

import java.io.UnsupportedEncodingException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dominFramework.core.typewrap.Dto;
import com.dominator.utils.system.Constants;
import com.dominator.utils.system.PropertiesLoader;
import com.dominator.utils.system.VerifyCodeUtil;
import com.dominator.utils.exception.ApiException;

import net.sf.json.JSONArray;

public class DianJiSmsUtils {
	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");

	/**
	 * 发送短信
	 * 
	 * @param phoneNum
	 * @param verifyCode
	 * @param type
	 * @param dto
	 * @throws ApiException
	 */
	public static void sendSms(Dto dto) throws ApiException {
		System.out.println("dianji发送短信开始");
		String content = "";
		switch (dto.getInteger("stype")) {
		case 1:
			content = propertiesLoader.getProperty("sms.notice.register_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 2:
			content = propertiesLoader.getProperty("sms.notice.login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 3:
			content = propertiesLoader.getProperty("sms.notice.find_login_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 4:
			content = propertiesLoader.getProperty("sms.notice.recharge_tpl");
			try {
				content = String.format(content, dto.getString("phoneNum"), dto.getString("payMoney"),
						new String(dto.getString("lesson").getBytes("utf-8"), "ISO-8859-1"));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
			}
			break;
		case 5:
			content = propertiesLoader.getProperty("sms.notice.register_tpl");
			content = String.format(content, dto.getInteger("verifyCode"));
			break;
		case 6:
			content = propertiesLoader.getProperty("sms.notice.order_bean_tpl");
			content = String.format(content, dto.getString("phoneNum"), dto.getString("lesson"),
					dto.getString("usedStudyBean"), dto.getString("totalStudyBean"));
			break;
		}

		try {
			content = propertiesLoader.getProperty("sms.notice.sign") + content
					+ propertiesLoader.getProperty("sms.notice.tail_tpl");
			System.out.println(content);
			content = new String(content.getBytes("ISO-8859-1"), "utf-8");
			System.out.println(content);
		} catch (UnsupportedEncodingException e) {
			throw new ApiException(Constants.SYS_ERROR, "模版编码错误");
		}
		content = String.format(content, VerifyCodeUtil.getCode6());
		System.out.println(content);
		String res = "";
		System.out.println("dianji开始发送短信");
		res = SmsClientSend.sendSms(propertiesLoader.getProperty("sms.dianji.url"),propertiesLoader.getProperty("sms.dianji.user_code"),propertiesLoader.getProperty("sms.dianji.user_pass"), dto.getString("phoneNum"),content);
		System.out.println("短信报文："+res);
		System.out.println("dianji发送短信结束");
		System.out.println(res);
/*		String jsonStr = XmlConverUtil.xmltoJson(res);*/
/*		System.out.println(jsonStr);*/

		JSONObject json = (JSONObject) JSON.parse(res);
		JSONArray arry = JSONArray.fromObject( json.get("Rets").toString());
		if ("0".equals(arry.getJSONObject(0).get("Rspcode")+"")) {
			System.out.println("发送成功");
		} else {
			System.out.println("调用短信接口失败");
			throw new ApiException(Constants.SYS_ERROR, "调用短信接口失败");
		}

	}
	public static void main(String[] args) {
		String res = "{\"alipay_trade_query_response\":{\"code\":\"10000\",\"msg\":\"Success\",\"buyer_logon_id\":\"151****6460\",\"buyer_pay_amount\":\"0.00\",\"buyer_user_id\":\"2088412954230600\",\"invoice_amount\":\"0.00\",\"open_id\":\"20880023203588486967788682813160\",\"out_trade_no\":\"20170426191051\",\"point_amount\":\"0.00\",\"receipt_amount\":\"0.00\",\"send_pay_date\":\"2017-04-26 19:09:30\",\"total_amount\":\"0.01\",\"trade_no\":\"2017042621001004600222805139\",\"trade_status\":\"TRADE_SUCCESS\"},\"sign\":\"fpdzWThlaC5BxW5lNvPBWxgJJIn8DcmwDdHwYlBcvJqLQqDPEJgkcv1BC7ui8NdURe9zgG4ca6bv7RiMMNRRIYkrfhp+rLvuxF2whq6koiF+W3kIHTHuETeecbNfKSIebGXCqnAAXISkebR26ll6b1FLFqOJ+Sj5zNlc8iOgLZM=\"}";
		JSONObject json = (JSONObject) JSON.parse(res);
		System.out.println(json.get("alipay_trade_query_response").toString());
	}
}
